<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login page</title>
</head>
    <body>
    <?php
    	require_once 'functions.php';
    	
		session_start();
		
		if (isset($_SESSION['username'])){
			header("Location: index.php");
		}
		
		$database = "tractors";
		$host = "localhost";
		$db_user = "root";
		
		$link = mysqli_connect($host, $db_user, "", $database) or die("Could not connect to $database" . mysql_error());
		
	    if (isset($_POST['login'])) {
			$user = $_POST['username'];
			$pass = $_POST['password'];
			$enc_pass = md5($pass);
			
			if (user_exists($user, $enc_pass, $link)) {
				$_SESSION['username'] = $user;
				$_SESSION['password'] = $pass;
				
				if (isset($_POST['remember'])){
					setcookie('username', $user);
					setcookie('password', $enc_pass);
				}
				
				header("Location: index.php");
			}
			else {
				echo "<p>Username $user does not exist!</p>";
			}
		}
	?>
	<form method="post">
		<label for "username">Username: </label><input type="text" name="username"/>
		<label for "password">Password: </label><input type="password" name="password"/>
		<p><input type="checkbox" name="remember"/> Remember me </p>
		<p><input type="submit" value="login" name="login"/></p>
		<p>or <a href="register.php">Register</a></p>
	</form>
    </body>
</html>